export function needAttr(obj, attr, val) {
  if (val) {
    obj.dataset[attr] = val;
  }
  return obj.dataset[attr];
}

export function shuffle(arr) {
  // const res = Array.from(arr);
  const res = arr.slice();
  return res.sort(() => [0, 1, -1][Math.floor(Math.random() * 3)]);
}

export function random(max) {
  return Math.floor(Math.random() * max);
}

export function debounce(fn, delay) {
  let timer;
  return (...args) => {
    timer && clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
}

export function formatTime(stamp) {
    const time = Math.floor(stamp);
    const m = Math.floor(time / 60);
    let s = time % 60;
    s < 10 ? s = `0${s}` : '';
    return `${m}:${s}`;
}
